<template>
  <el-dialog
    v-el-drag-dialog
    title="退单"
    :visible.sync="visible"
    width="600px"
    @closed="reset"
  >
    <el-form ref="dataForm" class="form" :model="temp" label-position="right" label-width="80px" style="margin: 0 50px;">
      <el-form-item label="客户名称">
        <el-input v-model="csmName" disabled />
      </el-form-item>
      <el-form-item label="客户电话">
        <el-input v-model="csmPhone" disabled />
      </el-form-item>
      <el-form-item class="block" label="退单理由" prop="remark">
        <el-input v-model="temp.remark" type="textarea" :rows="3" maxlength="255" show-word-limit />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">
        取消
      </el-button>
      <el-button :loading="$loadingPlugin.submit" type="primary" @click="success">
        确定
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { queryOrderBack } from '@/api'

  export default {
    name: 'back-order',
    props: {
      get3orderHistory: {
        type: Function
      }
    },
    data() {
      return {
        visible: false,
        orderCode: '',
        csmName: '',
        csmPhone: '',
        temp: {
          remark: ''
        },
        detail: {}
      }
    },
    methods: {
      open(temp) {
        return new Promise(resolve => {
          this.resolve = resolve
          this.visible = true
          if (temp) {
            this.detail = temp
            this.csmName = temp.csmName
            this.csmPhone = temp.csmPhone
          }
        })
      },
      success() {
        this.$refs.dataForm.validate(valid => {
          if (!valid) return
          const params = { ...this.temp }
          params.orderCode = this.detail.orderCode
          params.ownerId = this.detail.ownerId
          this.submit(params)
          this.get3orderHistory()
        })
      },
      async submit(params) {
        await queryOrderBack(params)
        this.resolve()
        this.visible = false
        this.$notify({
          title: '提示',
          message: '操作成功!',
          type: 'success',
          duration: 2000
        })
      },
      reset() {
        this.temp = {
          remark: ''
        }
        this.$nextTick(() => {
          this.$refs.dataForm.clearValidate()
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
.form {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  & /deep/ .el-form-item {
    width: 48%;
    .el-form-item__content > .el-select {
      width: 100%;
    }
  }
  .block {
    width: 100%;
  }
}
</style>
